{extend name="common/base"/}
<!-- 主体 -->
{block name="body"}
<style>
	.upload-img {
		width: 120px;
		height: 90px;
		overflow: hidden;
		position: relative;
		border: 1px solid #eee;
		padding: 1px;
		margin: 5px;
		float: left;
	}

	.upload-close {
		position: absolute;
		top: 1px;
		right: 1px;
	}
</style>
<form class="layui-form p-4">
	<h3 class="pb-3">编辑积分商品</h3>
	<table class="layui-table layui-table-form">
		<tr>
			<td class="layui-td-gray-2">商品名称<font>*</font></td>
			<td><input type="text" name="title" lay-verify="required" lay-reqText="请完善商品名称"
					   value="{$detail.title}" autocomplete="off" placeholder="请输入商品名称" class="layui-input"></td>
			<td class="layui-td-gray-2">积分<font>*</font></td>
			<td><input type="text" name="price" lay-verify="required" lay-reqText="请完善积分" value="{$detail.price}"
					   autocomplete="off" placeholder="请输入积分" class="layui-input"></td>
			<td class="layui-td-gray-2">状态</td>
			<td>
				<input type="radio" name="status" value="1" title="上架" {eq name="$detail.status" value="1" }
					   checked{/eq}>
				<input type="radio" name="status" value="0" title="下架" {eq name="$detail.status" value="0" }
					   checked{/eq}>
			</td>
		</tr>
		<tr>
			<td class="layui-td-gray-2">是否包邮</td>
			<td>
				<input type="radio" name="is_mail" value="0" title="否" {eq name="$detail.is_mail" value="0" }
					   checked{/eq}>
				<input type="radio" name="is_mail" value="1" title="是" {eq name="$detail.is_mail" value="1" }
					   checked{/eq}>
			</td>
			<td class="layui-td-gray-2">运费</td>
			<td><input type="text" name="yun_money" value="{$detail.yun_money}" autocomplete="off"
					   placeholder="请输入运费" class="layui-input"></td>
		</tr>
		<tr>
			<td class="layui-td-gray-2">缩略图<font>*</font></td>
			<td colspan="5" style="vertical-align:top">
				<div class="layui-upload">
					<button type="button" class="layui-btn layui-btn-sm" id="upload_btn_thumb">选择上传图片</button>
					<div class="layui-upload-list" id="upload_box_thumb">
						<img src="{$detail.thumb}"
							 onerror="javascript:this.src='{__GOUGU__}/gougu/images/nonepic600x360.jpg';this.onerror=null;"
							 style="width:200px;max-width:200px"/>
						<input type="hidden" name="thumb" value="{$detail.thumb}" lay-verify="required"
							   lay-reqText="请完善缩略图">
					</div>
				</div>
			</td>
		</tr>
		<tr>
			<td class="layui-td-gray">商品图集</td>
			<td colspan="7">
				<div class="layui-upload">
					<button type="button" class="layui-btn layui-btn-sm" id="uploadBtn2">上传轮播图</button>
					<div class="layui-upload-list" id="demo2">
						<input type="hidden" name="banner" value="{$detail.banner}">
						{notempty name="detail.banner"}
						{volist name="detail.banner_array" id="vo"}
						<div class="upload-img img-cover" id="uploadImg{$vo}"><div class="gg-img-cover cover-4-3"><img src="{$vo|get_file}"><div class="upload-close"><a class="layui-btn layui-btn-xs layui-btn-danger" lay-event="delimg" data-id="{$vo}">删除</a></div></div></div>
						{/volist}
						{/notempty}
					</div>
				</div>
			</td>
		</tr>
		<tr>
			<td class="layui-td-gray-2">商品详情</td>
			<td colspan="5">
				<textarea class="layui-textarea" id="container_content">{$detail.content}</textarea>
			</td>
		</tr>
	</table>
	<div class="py-3">
		<input type="hidden" name="id" value="{$detail.id}"/>
		<button class="layui-btn layui-btn-normal" lay-submit="" lay-filter="webform">立即提交</button>
		<button type="reset" class="layui-btn layui-btn-primary">重置</button>
	</div>
</form>
{/block}
<!-- /主体 -->

<!-- 脚本 -->
{block name="script"}
<script>
	// 查找指定的元素在数组中的位置
	Array.prototype.indexOf = function (val) {
		for (var i = 0; i < this.length; i++) {
			if (this[i] == val) {
				return i;
			}
		}
		return -1;
	};
	// 通过索引删除数组元素
	Array.prototype.remove = function (val) {
		var index = this.indexOf(val);
		if (index > -1) {
			this.splice(index, 1);
		}
	};
	var moduleInit = ['tool', 'tinymce'];

	function gouguInit() {
		var form = layui.form, tool = layui.tool;

		form.on('radio(goodsType)', function (data) {
			var value = data.value;   //  当前选中的value值
			if (value != '1') {
				$('#song').show();
			} else {
				$('#song').hide();
			}
		});

		//上传缩略图
		var upload_thumb = layui.upload.render({
			elem: '#upload_btn_thumb',
			url: '/admin/api/upload',
			done: function (res) {
				//如果上传失败
				if (res.code == 1) {
					return layer.msg('上传失败');
				}
				//上传成功
				$('#upload_box_thumb input').attr('value', res.data.filepath);
				$('#upload_box_thumb img').attr('src', res.data.filepath);
			}
		});
		//banner图上传
		var uploadInst2 = layui.upload.render({
			elem: '#uploadBtn2'
			, url: '/admin/api/upload'
			, done: function (res) {
				//如果上传失败
				if (res.code == 1) {
					return layer.msg('上传失败');
				}
				//上传成功
				var idsStr = $('#demo2 input').val();
				var idsArray = [];
				if (idsStr != '') {
					idsArray = idsStr.split(",");
				}
				idsArray.push(res.data.id);
				$('#demo2 input').attr('value', idsArray.join(','));
				$('#demo2').append('<div class="upload-img img-cover" id="uploadImg' + res.data.id + '"><div class="gg-img-cover cover-4-3"><img src="' + res.data.filepath + '"><div class="upload-close"><a class="layui-btn layui-btn-xs layui-btn-danger" lay-event="delimg" data-id="' + res.data.id + '">删除</a></div></div>');
			}
		});
		$('#demo2').on('click', '[lay-event="delimg"]', function () {
			var _id = $(this).data('id');
			var idsStr = $('#demo2 input').val();
			var idsArray = [];
			if (idsStr != '') {
				idsArray = idsStr.split(",");
			}
			console.log(_id)
			console.log(idsArray)
			idsArray.remove(_id);
			$('#demo2 input').attr('value', idsArray.join(','));
			$('#uploadImg' + _id).remove();
		})
		//商品详情富文本编辑器
		var edit = layui.tinymce.render({
			selector: '#container_content',
			height: 500
		});
		//监听提交
		form.on('submit(webform)', function (data) {
			data.field.content = tinyMCE.editors['container_content'].getContent();
			if (data.field.content == '') {
				layer.msg('请先完善商品详情内容');
				return false;
			}
			let callback = function (e) {
				layer.msg(e.msg);
				if (e.code == 0) {
					tool.sideClose(1000);
				}
			}
			tool.post("/admin/score_goods/edit", data.field, callback);
			return false;
		});
	}
</script>
{/block}
<!-- /脚本 -->